home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 003-desktop.lzm / usr / bin / khc_docbookdig.pl < prev    next >
Encoding:
Perl Script  |  2008-10-25  |  5.1 KB  |  222 lines

  1. #!/usr/bin/perl
  2. #
  3. #  Wrapper script for creating search indices for htdig.
  4. #
  5. #  This file is part of KHelpcenter.
  6. #
  7. #  Copyright (C) 2002  SuSE Linux AG, Nuernberg
  8. #
  9. #  Author: Cornelius Schumacher <cschum@suse.de>
  10. #
  11. #  This program is free software; you can redistribute it and/or modify
  12. #  it under the terms of the GNU General Public License as published by
  13. #  the Free Software Foundation; either version 2 of the License, or
  14. #  (at your option) any later version.
  15. #
  16. #  This program is distributed in the hope that it will be useful,
  17. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. #  GNU General Public License for more details.
  20. #
  21. #  You should have received a copy of the GNU General Public License
  22. #  along with this program; if not, write to the Free Software
  23. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  24.  
  25. use strict;
  26.  
  27. use Getopt::Long;
  28.  
  29. my $htdigdata = "/srv/www/htdig/common/";
  30. my $htdigbin = "/usr/bin";
  31. my $kdeprefix = "/usr";
  32. chomp $kdeprefix;
  33.  
  34. my $dbg = 1;
  35.  
  36. my ($indexdir, $docpath, $identifier, $lang, $help );
  37.  
  38. GetOptions (
  39.   'indexdir=s' => \$indexdir,
  40.   'docpath=s' => \$docpath,
  41.   'identifier=s' => \$identifier,
  42.   'lang=s' => \$lang,
  43.   'help' => \$help,
  44. );
  45.  
  46. if ( $help ) {
  47.   usage();
  48. }
  49.  
  50. if ( !$indexdir || !$docpath || !$identifier ) {
  51.   print STDERR "Missing arguments.\n";
  52.   usage();
  53. }
  54.  
  55. &dbg( "INDEXDIR: $indexdir" );
  56.  
  57. if ( !$lang ) { $lang = "en"; }
  58.  
  59. my $tmpdir = "$indexdir/$identifier.tmp";
  60. if ( ! -e $tmpdir ) {
  61.   mkdir $tmpdir;
  62. }
  63.  
  64. print "Creating index for <b>'$identifier'</b>\n";
  65.  
  66. my $htdigconf = $indexdir;
  67. my $htdigdb = $indexdir;
  68.  
  69. my $conffile = "$htdigconf/$identifier.conf";
  70.  
  71. my $commondir = "$htdigdata/$lang";
  72. if ( !$lang || !-e $commondir ) {
  73.   $commondir = "$htdigdata/en";
  74. }
  75. if ( !-e $commondir ) { $commondir = $htdigdata; }
  76.  
  77. my $locale;
  78. if ( $lang eq "de" ) { $locale = "de_DE"; }
  79. else { $locale = $lang; }
  80.  
  81. my $startfile = "$tmpdir/index.html";
  82.  
  83. if ( !open( START, ">$startfile" ) ) {
  84.   print STDERR "Unable to open '$startfile' for writing.\n";
  85.   exit 1;
  86. }
  87.  
  88. $ENV{ PATH } = '/bin:/usr/bin';
  89. $ENV{ CDPATH } = '';
  90. $ENV{ ENV } = '';
  91.  
  92. my $findpath = "$kdeprefix/share/doc/HTML/$lang/";
  93. my $findcmd = "find $findpath -name index.docbook";
  94.  
  95. print STDERR "FINDCMD: $findcmd\n";
  96.  
  97. if ( !open FIND, "$findcmd|" ) {
  98.   print STDERR "Unable to find docs.\n";
  99.   exit 1;
  100. }
  101. while ( <FIND> ) {
  102.   chomp;
  103.   my $path = $_;
  104.   $path =~ /$findpath(.*)\/index.docbook$/;
  105.   my $app = $1;
  106.   print START "<a href=\"help://$app/index.docbook\">$path</a>\n";
  107. }
  108. close START;
  109.  
  110. my $mimetypefile = "$tmpdir/htdig_mime";
  111. if ( !open( MIME, ">$mimetypefile" ) ) {
  112.   print STDERR "Unable to open '$mimetypefile' for writing.\n";
  113.   exit 1;
  114. }
  115. print MIME << "EOT";
  116. text/html       html
  117. text/docbook    docbook
  118. EOT
  119. close MIME;
  120.  
  121. my $parserfile = "$tmpdir/docbookparser";
  122. if ( !open( PARSER, ">$parserfile" ) ) {
  123.   print STDERR "Unable to open '$parserfile' for writing.\n";
  124.   exit 1;
  125. }
  126. print PARSER << "EOT";
  127. #! /bin/sh
  128.  
  129. file=\$1
  130. shift
  131. mime=\$1
  132. shift
  133.  
  134. if test "\$#" -gt 0; then
  135.   orig=\${1/file:\\//}
  136.   shift
  137. fi
  138.  
  139. case "\$orig" in
  140.   help:/*)
  141.     orig=\${orig/help:\\//}
  142.     orig=\${orig/\/index.docbook/}
  143.     cd $kdeprefix/share/doc/HTML/en/\$orig
  144.     file=index.docbook
  145.     ;;
  146.   *)    
  147.     file=\$orig
  148.     cd `dirname \$orig`
  149.     ;;
  150. esac
  151.  
  152. echo "t    apptitle"
  153. $kdeprefix/bin/meinproc --htdig "\$file"
  154. EOT
  155. close PARSER;
  156. chmod 0755, $parserfile;
  157.  
  158. if ( !open( CONF, ">$conffile" ) ) {
  159.   print STDERR "Unable to open '$conffile' for writing.\n";
  160.   exit 1;
  161. }
  162. print CONF << "EOT";
  163. # htdig configuration for doc '$identifier'
  164. #
  165. # This file has been automatically created by KHelpcenter
  166. common_dir:        $commondir
  167. locale:                 $locale
  168. database_dir:           $htdigdb
  169. database_base:        \${database_dir}/$identifier
  170. local_urls:             help://=$kdeprefix/share/doc/HTML/en/ file://=/
  171. local_urls_only:        true
  172. limit_urls_to:          file:// help:/
  173. ignore_noindex:         true
  174. max_hop_count:          4
  175. robotstxt_name:         kdedig
  176. compression_level:      6
  177. template_map:           Long long $kdeprefix/share/apps/khelpcenter/searchhandlers/htdig/htdig_long.html
  178. search_algorithm:       exact:1 prefix:0.8
  179. maximum_pages:          1
  180. matches_per_page:       10
  181. start_url:              file://$tmpdir/index.html
  182. external_parsers:       text/docbook $parserfile
  183. valid_extensions:       .docbook .html
  184. mime_types:             $mimetypefile
  185. EOT
  186. close CONF;
  187.  
  188. my $ret = system( "$htdigbin/htdig", "-v", "-s", "-i", "-c", $conffile );
  189. if ( $ret != 0 ) {
  190.   print STDERR "htdig failed\n";
  191. } else {
  192.   $ret = system( "$htdigbin/htmerge", "-c", $conffile );
  193.   if ( $ret != 0 ) { print STDERR "htmerge failed\n"; }
  194. }
  195.  
  196. if ( $ret == 0 ) {
  197.   my $existsfile = "$indexdir/$identifier.exists";
  198.  
  199.   if ( !open( EXISTS, ">$existsfile" ) ) {
  200.     print STDERR "Unable to open '$existsfile' for writing.\n";
  201.     exit 1;
  202.   }
  203.   print EXISTS "$identifier\n";
  204.   close EXISTS;
  205.  
  206.   print "Finished successfully.\n";
  207. }
  208.  
  209. exit $ret;
  210.  
  211. sub dbg($)
  212. {
  213.   $dbg && print STDERR shift, "\n";
  214. }
  215.  
  216. sub usage()
  217. {
  218.   print "Usage: khc_docbookdig.pl --indexdir <indexdir> --docpath <path> ";
  219.   print "--identifier <identifier>\n";
  220.   exit 1;
  221. }
  222.